﻿using ServiceCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Entity;

namespace Customer.Bussiness_Service
{

    public interface Ibug_submit_service
    {
        ResultObjModel sub_bug(save_bug Params);
        ResultListModel<search_bug_submit> getpagelist(Base_List_Params query);
        ResultObjModel del(string bug_submit_id);

        ResultObjModel change_state(string bug_submit_id, submit_state? submit_state);
    }
    public class bug_submit_service : Ibug_submit_service
    {
        private readonly IToole_DI _tool;

        public bug_submit_service(IToole_DI Tool)
        {
            _tool = Tool;
        } 
        /// <summary>
        /// 提交bug信息
        /// </summary>
        /// <returns></returns>
        public ResultObjModel sub_bug(save_bug Params)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (string.IsNullOrWhiteSpace(Params.bug_des))
                {
                    result.code = 1;
                    result.msg = "描述不能为空";
                    return result;
                }
                if (Params.bug_pic == null)
                {
                    Params.bug_pic = new string[] { };
                }
                bug_submit mod = new bug_submit();

                int tran_rows = 0;
                if (string.IsNullOrWhiteSpace(Params.bug_submit_id))
                {
                    mod.bug_type = Params.bug_type;
                    mod.bug_des = Params.bug_des;
                    mod.bug_pics = Newtonsoft.Json.JsonConvert.SerializeObject(Params.bug_pic);
                    mod.bug_submit_id = _tool.di_getid.IGet_SnowFlake_Id;
                    mod.submit_state = submit_state.未处理;
                    mod.sub_date = DateTime.Now;
                    mod.uid = _tool.di_user.USER.uid;
                    mod.uname = _tool.di_user.USER.u_name;
                    mod.lv = 0;
                    tran_rows = _tool.di_db.Add(mod);
                }
                else
                {
                    var check_mod = _tool.di_db.GetList<bug_submit>("select top 1 * from bug_submit where bug_submit_id=@bug_submit_id", new
                    {
                        Params.bug_submit_id
                    }).FirstOrDefault();
                    if (check_mod == null)
                    {
                        result.code = 1;
                        result.msg = "修改问题不存在";
                        return result;
                    }
                    if (check_mod.submit_state != submit_state.未处理 && check_mod.submit_state != submit_state.解决中)
                    {
                        result.code = 1;
                        result.msg = "BUG修改中或已产生结论,无法修改";
                        return result;
                    }
                    check_mod.bug_type = Params.bug_type;
                    check_mod.bug_des = Params.bug_des;
                    check_mod.bug_pics = Newtonsoft.Json.JsonConvert.SerializeObject(Params.bug_pic);
                    tran_rows = _tool.di_db.Update<bug_submit>(new string[] {
                        "bug_type","bug_des","bug_pics"
                    }, new string[] {
                         "bug_submit_id=@bug_submit_id"
                    }, check_mod);
                }
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "保存成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "保存失败";
                }
                return result;
            });
        }
        /// <summary>
        ///  列表查询
        /// </summary>
        /// <returns></returns>
        public ResultListModel<search_bug_submit> getpagelist(Base_List_Params query)
        {
            return CoreStartDo.ResultAction<ResultListModel<search_bug_submit>>(result =>
            {
                string sql = "select * from bug_submit";
                string where = " where 1=1";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                if (!string.IsNullOrWhiteSpace(query.SerchKeyword))
                {
                    where += " and uname like @SerchKeyword";
                    query_dy.SerchKeyword = $"%{query.SerchKeyword}%";
                }
                return _tool.di_db.GetPageList<search_bug_submit>(sql, where, "submit_state asc,bug_submit_id", query_dy, query.IsPage, query.page, query.pagesize, "desc"); 
            });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="bug_submit_id"></param>
        /// <returns></returns>
        public ResultObjModel del(string bug_submit_id)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (string.IsNullOrWhiteSpace(bug_submit_id))
                {
                    result.code = 1;
                    result.msg = "bug_submit_id不能为空";
                    return result;
                }
                int db_rows = _tool.di_db.do_Execute("delete from bug_submit where bug_submit_id=@bug_submit_id", new { bug_submit_id });
                if (db_rows > 0)
                {
                    result.code = 0;
                    result.msg = "删除成功"; 
                }
                else {
                    result.code = 1;
                    result.msg = "删除失败"; 
                }
                return result;
            });
        }
        /// <summary>
        /// 变更状态
        /// </summary>
        /// <returns></returns>
        public ResultObjModel change_state(string bug_submit_id, submit_state? submit_state)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (string.IsNullOrWhiteSpace(bug_submit_id))
                {
                    result.code = 1;
                    result.msg = "bug_submit_id不能为空";
                    return result;
                }
                if (!submit_state.HasValue)
                {
                    result.code = 1;
                    result.msg = "修改状态不能为空";
                    return result;
                }
                var check_mod = _tool.di_db.GetList<bug_submit>("select top 1 * from bug_submit where bug_submit_id=@bug_submit_id", new
                {
                    bug_submit_id
                }).FirstOrDefault();
                if (check_mod == null)
                {
                    result.code = 1;
                    result.msg = "修改问题不存在";
                    return result;
                }
                if (check_mod.submit_state == Entity.submit_state.已完成 || check_mod.submit_state == Entity.submit_state.设计如此 || check_mod.submit_state == Entity.submit_state.忽略)
                {
                    result.code = 1;
                    result.msg = "BUG已产生解决,无法变更状态";
                    return result;
                }
                DateTime? end_time = null;
                if (submit_state == Entity.submit_state.已完成 || submit_state == Entity.submit_state.设计如此 || submit_state == Entity.submit_state.忽略)
                {
                    end_time = DateTime.Now;
                }
                int tran_rows = _tool.di_db.do_Execute("update bug_submit set submit_state=@submit_state,end_uid=@end_uid,end_uname=@end_uname,end_time=@end_time where bug_submit_id=@bug_submit_id", new
                {
                    submit_state,
                    bug_submit_id,
                    end_uid = _tool.di_user.USER.uid,
                    end_uname = _tool.di_user.USER.u_name,
                    end_time
                });
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "保存成功"; 
                }
                else {
                    result.code = 1;
                    result.msg = "保存失败"; 
                } 
                return result;
            });
        }
    }
}
